﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Modularity;
using Volo.Abp.Testing;
using Volo.Abp.Uow;


namespace Sample.Novel.TestBase
{
   public abstract class NovelTestBase<TStratupModule>:AbpIntegratedTest<TStratupModule> where TStratupModule:IAbpModule
    {
        protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options)
        {
            options.UseAutofac();
        }
        //开启工作单元的几个方法：
        protected virtual async Task WithUnitOfWorkAsync(AbpUnitOfWorkOptions options, Func<Task> action)
        {
           
            using (var scope=ServiceProvider.CreateScope())
            {
                var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
                using (var uow=uowManager.Begin(options))
                {
                    await action();
                    await uow.CompleteAsync();

                }
            }
        }
        protected virtual async Task<TResult> WithUnitOfWorkAsync<TResult>(AbpUnitOfWorkOptions options, Func<Task<TResult>> action)
        {
            using (var scope = ServiceProvider.CreateScope())
            {
                var uowManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>();
                using (var uow = uowManager.Begin(options))
                {
                var result= await action();
                    await uow.CompleteAsync();
                    return result;
                }
            }

        }

        protected virtual  Task WithUnitOfWorkAsync(Func<Task> action)
        {  
                return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(),action);
        }

        protected virtual Task<TResult> WithUnitOfWorkAsync<TResult>(Func<Task<TResult>> action)
        {
            return WithUnitOfWorkAsync(new AbpUnitOfWorkOptions(), action);
        }
    }
}
